{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.chdir(\"../\")\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "from matplotlib import colormaps as cm\n",
    "\n",
    "cmap = cm.get_cmap(\"viridis\")\n",
    "\n",
    "from pathlib import Path\n",
    "\n",
    "import hydra\n",
    "\n",
    "hydra.core.global_hydra.GlobalHydra.instance().clear()\n",
    "hydra.initialize(version_base=\"1.3\", config_path=\"../configs/\")\n",
    "cfg = hydra.compose(config_name=\"eval.yaml\")\n",
    "\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = Path(\"logs/calibrate/runs\")\n",
    "ckpt_path = root_dir / \"<experiment folder>/cmodel.ckpt\"\n",
    "\n",
    "model = hydra.utils.instantiate(cfg.model)\n",
    "# load weights from checkpoint\n",
    "checkpoint = torch.load(ckpt_path, map_location=device)\n",
    "model.load_state_dict(checkpoint[\"state_dict\"])\n",
    "nc_curves = checkpoint[\"nc_curves\"]\n",
    "print(nc_curves.shape)\n",
    "model = model.to(device)\n",
    "print(model.device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The cell below picks out certain pixel locations and plots the corresponding calibration curves."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coords_x = torch.tensor([4, 20, 150, 160, 170, 300, 316])\n",
    "coords_y = torch.tensor([4, 20, 120, 220, 236])\n",
    "grid = torch.meshgrid(coords_x, coords_y, indexing=\"ij\")\n",
    "\n",
    "class_i = 1\n",
    "img = torch.ones_like(nc_curves[0, 0])\n",
    "img[grid[1], grid[0]] = 0\n",
    "\n",
    "plt.imshow(img, cmap=\"gray\", vmin=0, vmax=1)\n",
    "plt.show()\n",
    "plt.clf()\n",
    "\n",
    "nc_curves_sub = nc_curves[:, class_i, grid[1], grid[0]]\n",
    "\n",
    "sub_w = nc_curves_sub.shape[2]\n",
    "sub_h = nc_curves_sub.shape[1]\n",
    "\n",
    "fig, axes = plt.subplots(nrows=sub_w, ncols=sub_h, figsize=(10, 10))\n",
    "\n",
    "# loop over the last two dimensions\n",
    "for i in range(sub_w):\n",
    "    for j in range(sub_h):\n",
    "        ncss = nc_curves_sub[:, j, i]\n",
    "        axes[i, j].plot(ncss)\n",
    "        axes[i, j].set_ylim([0, 1])\n",
    "        axes[i, j].set_xlim([0, ncss.shape[0]])\n",
    "        axes[i, j].axis(\"off\")\n",
    "\n",
    "fig.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "monseg",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
